package com.zx.rabbitmq.springboot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitMq 配置类
 * <p>TTL 过期时间设置</p>
 *
 * @author Zx
 * @date 2021/5/31 17:54
 */
@Configuration
public class TTLRabbitMqConfiguration {
    /**
     * 1 声明交换机(广播模式)
     *
     * @return
     */
    @Bean
    public DirectExchange ttlDirectExchange() {
        return new DirectExchange("ttl_direct_exchange", true, false);
    }

    /**
     * 2 声明队列
     *
     * @return
     */
    @Bean
    public Queue ttlDirectQueue() {
        // 设置过期时间
        HashMap<String, Object> args = new HashMap<>(1);
        args.put("x-message-ttl", 5000);
        // 设置最大消息记录数
//        args.put("x-max-length", 5);
        // 设置死信队列接盘
        args.put("x-dead-letter-exchange", "dead_direct_exchange");
        // 设置路由Key
        args.put("x-dead-letter-routing-key", "dead");
        return new Queue("ttl.direct.queue", true, false, false, args);
    }

    /**
     * 2 声明队列
     *
     * @return
     */
    @Bean
    public Queue ttlDirectMessageQueue() {
        return new Queue("ttl.message.direct.queue", true);
    }


    /**
     * 3 完成关系绑定, 队列交换机绑定
     *
     * @return
     */
    @Bean
    public Binding ttlDirectBinding() {
        return BindingBuilder.bind(ttlDirectQueue()).to(ttlDirectExchange()).with("ttl");
    }

    /**
     * 3 完成关系绑定, 队列交换机绑定
     *
     * @return
     */
    @Bean
    public Binding ttlMessageDirectBinding() {
        return BindingBuilder.bind(ttlDirectMessageQueue()).to(ttlDirectExchange()).with("ttlMessage");
    }


}
